<?php
namespace app\data\service;
use think\admin\Exception;
use think\admin\extend\JsonRpcClient;
use think\admin\Library;
use think\admin\storage\LocalStorage;
use think\exception\HttpResponseException;
use AlibabaCloud\SDK\Iot\V20180120\Iot;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use AlibabaCloud\Tea\Exception\TeaError;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Iot\V20180120\Models\ListOTAUnfinishedTaskByDeviceRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\ConfirmOTATaskRequest;
use AlibabaCloud\SDK\Iot\V20180120\Models\ReupgradeOTATaskRequest;
class OtaService
{
    private $client;
    public function __construct( )
    {
        $config = new Config([]);
        $config->accessKeyId = config('iot.accessKeyId');// 您的AccessKey ID。
        $config->accessKeySecret = config('iot.accessKeySecret');// 您的AccessKey Secret。
        $config->regionId = config('iot.regionId');// 您的可用区ID。
        $this->client = new Iot($config);
    }
    //查询升级信息
    public function listUnfinishedTask($data){
        try {
            $request = new ListOTAUnfinishedTaskByDeviceRequest($data);
            $response =  $this->client->ListOTAUnfinishedTaskByDevice($request);
            $res['err_code']=0;
            $res['data'] = $response;
        } catch (TeaUnableRetryError $e) {
            $res['err_code']=1;
            $res['data'] = $e->getMessage();
        }
        return $res;
    }
    //确认升级
    public function confirmUpdate ($data){
        try {
            $request = new ConfirmOTATaskRequest($data);
            $response =  $this->client->ConfirmOTATask($request);
            $res['err_code']=0;
            $res['data'] = $response;
        } catch (TeaUnableRetryError $e) {
            $res['err_code']=1;
            $res['data'] = $e->getMessage();
        }
        return $res;
    }
}